import 'package:flutter/material.dart';

import '../model/common_model.dart';
import 'webview.dart';

class SubNav extends StatelessWidget {
  final List<CommonModel> subNavList;

  const SubNav({super.key, required this.subNavList});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(6)),
      ),
      child: Padding(
        padding: const EdgeInsets.all(8),
        child: _items(context),
      ),
    );
  }

  _items(BuildContext context) {
    if (subNavList.isEmpty) return null;
    List<Widget> items = [];
    for (final item in subNavList) {
      items.add(_item(context, item));
    }
    int separate = (subNavList.length / 2 + 0.5).toInt();
    return Column(
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: items.sublist(0, separate),
        ),
        Padding(
          padding: const EdgeInsets.only(top: 10),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: items.sublist(separate, subNavList.length),
          ),
        ),
      ],
    );
  }

  _item(BuildContext context, CommonModel item) {
    return Expanded(
        flex: 1,
        child: GestureDetector(
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => WebView(
                  url: item.url!,
                  statusBarColor: item.statusBarColor,
                  hideAppBar: item.hideAppBar,
                ),
              ),
            );
          },
          child: Column(
            children: [
              Image.network(
                item.icon!,
                width: 18,
                height: 18,
              ),
              Padding(
                padding: const EdgeInsets.only(top: 4),
                child: Text(
                  item.title!,
                  style: const TextStyle(fontSize: 12),
                ),
              ),
            ],
          ),
        ));
  }
}
